package main

import "sort"

func main() {

}

//#link: https://leetcode-cn.com/problems/the-k-weakest-rows-in-a-matrix/
func kWeakestRows(mat [][]int, k int) []int {
	type pair struct {
		value int
		index int
	}
	pairs := make([]pair, 0)
	//统计每行中1的数量
	for index, m := range mat {
		num := 0
		for _, n := range m {
			if n == 0 {
				break
			}
			num++
		}
		pairs = append(pairs, pair{index: index, value: num})
	}

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i].value < pairs[j].value || (pairs[i].value == pairs[j].value && pairs[i].index < pairs[j].index)
	})

	result := make([]int, k)
	for i := 0; i < k; i++ {
		result[i] = pairs[i].index
	}
	return result
}
